package com.objectproject.main.service.subsystem;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.objectproject.main.dao.MyObjectDAOImpl;
import com.objectproject.main.dao.MyObjectHierarchyDAOImpl;
import com.objectproject.main.model.MyObject;
import com.objectproject.main.service.subsystem.policy.PolicyAndConfigurationRepository;

@Service
public class MyObjectHierarchyResolver {

	@Autowired
	MyObjectDAOImpl myObjectDAOImpl;

	@Autowired
	MyLocaleResolver myLocaleResolver;

	@Autowired
	MyObjectHierarchyDAOImpl myObjectHierarchyDAOImpl;

	public ArrayList<MyObject> getSuperObjectsOf(MyObject myObject) {
		return myObjectHierarchyDAOImpl.selectSuperObjects(myObject
				.getObjectId());
	}

	public ArrayList<MyObject> getSubObjectsOf(MyObject myObject) {
		return myObjectHierarchyDAOImpl
				.selectSubObjects(myObject.getObjectId());
	}

	public ArrayList<MyObject> getAncestorObjectsOf(MyObject myObject) {
		return myObjectHierarchyDAOImpl.selectAncestorObjects(myObject
				.getObjectId());
	}

	public ArrayList<MyObject> getDescendantObjectsOf(MyObject myObject) {
		return myObjectHierarchyDAOImpl.selectDescendantObjects(myObject
				.getObjectId());
	}

	public ArrayList<MyObject> getIntefacesOf(MyObject myObject) {
		return myObjectHierarchyDAOImpl.selectInterfaceObjects(myObject
				.getObjectId());
	}

	public ArrayList<MyObject> getFinalObjectsOf(MyObject myObject) {
		return myObjectHierarchyDAOImpl.selectFinalObjects(myObject
				.getObjectId());
	}

	public boolean isSuperObjectOf(MyObject myObject, MyObject targetObject) {
		ArrayList<Long> superObjectIds = myObjectHierarchyDAOImpl
				.selectSuperObjectIds(targetObject.getObjectId());
		if (superObjectIds.contains(myObject.getObjectId()))
			return true;
		return false;
	}

	public boolean isSubObjectOf(MyObject myObject, MyObject targetObject) {
		ArrayList<Long> subObjectIds = myObjectHierarchyDAOImpl
				.selectSubObjectIds(targetObject.getObjectId());
		if (subObjectIds.contains(myObject.getObjectId()))
			return true;
		return false;
	}

	public MyObject getRootObject() {
		return myObjectDAOImpl
				.selectMyObjectById(PolicyAndConfigurationRepository.ROOT_OBJECT_ID);
	}

	public boolean isInstanceOf(MyObject myObject, MyObject targetObject) {
		ArrayList<Long> descendantObjectIds = myObjectHierarchyDAOImpl
				.selectDescendantObjectIds(targetObject.getObjectId());
		if (descendantObjectIds.contains(myObject.getObjectId()))
			return true;
		return false;
	}

	public boolean hasSuperObject(MyObject myObject) {

		return false;
	}

	/**
	 * calculate object's depth. before this method called, you had to register super objects of that
	 * 
	 * @return depth of object
	 */
	@Deprecated
	public int calculateDepthOf(MyObject myObject) {
		ArrayList<MyObject> superObjects = getSuperObjectsOf(myObject);
		int currentMaxDepth = 0;
		if (superObjects.size() > 0) {
			for (int i = 0; i < superObjects.size(); i++) {

				if (superObjects.get(i).getDepth() > currentMaxDepth)
					currentMaxDepth = superObjects.get(i).getDepth();
			}
			return currentMaxDepth + 1;
		} else {
			return 0;
		}
	}

}
